package com.springboot.server.controller;


import com.springboot.server.pojo.Admin;
import com.springboot.server.pojo.RespBean;
import com.springboot.server.pojo.Role;
import com.springboot.server.service.IAdminService;
import com.springboot.server.service.IRoleService;
import com.springboot.server.utils.FastDFSUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author XuMingYang
 * @since 2023-02-06
 */
@RestController
@RequestMapping("/system/admin")
public class AdminController {

    @Autowired
    private IAdminService adminService;
    @Autowired
    private IRoleService roleService;

    @ApiOperation(value = "获取所有操作员信息")
    @GetMapping("/")
    public List<Admin> getAllAdmins(String keywords) {
        return adminService.getAllAdmins(keywords);
    }


    @ApiOperation(value = "更新操作员信息")
    @PutMapping("/")
    public RespBean updateAdmin(@RequestBody Admin admin) {
        if (adminService.updateById(admin)) {
            return RespBean.success("更新成功！");
        }
        return RespBean.error("更新失败！");
    }


    @ApiOperation(value = "删除操作员信息")
    @DeleteMapping("/{id}")
    public RespBean deleteAdmin(@PathVariable Integer id) {
        if (adminService.removeById(id)) {
            return RespBean.success("删除成功！");
        }
        return RespBean.error("删除失败！");
    }

    @ApiOperation(value = "获取操作员角色")
    @GetMapping("/roles")
    public List<Role> getAllRoles() {
        return roleService.list();
    }

    @ApiOperation(value = "更新操作员角色")
    @PutMapping("/role")
    public RespBean updateAdminRole(Integer adminId, Integer[] rids) {
        return adminService.updateAdminRole(adminId, rids);
    }

    @ApiOperation(value = "更新用户头像")
    @ApiImplicitParams({@ApiImplicitParam(name = "file", value = "头像", dataType =
            "MultipartFile")})
    @PostMapping("/admin/userface")
    public RespBean updateHrUserFace(MultipartFile file, Integer id, Authentication
            authentication) {
        //获取上传文件地址
        String[] fileAbsolutePath = FastDFSUtil.upload(file);
        String url = FastDFSUtil.getTrackerUrl() + fileAbsolutePath[0] + "/" +
                fileAbsolutePath[1];
        return adminService.updateAdminUserFace(url, id, authentication);
    }
}
